package com.pagepub.expo.action.system;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.pagepub.expo.entity.AppFunction;
import com.pagepub.expo.entity.RoleFunction;
import com.pagepub.expo.manager.AppFunctionMng;
import com.pagepub.expo.manager.RoleFunctionMng;

public class FunctionTreeAction extends ActionSupport {

	private static final long serialVersionUID = 201005091010L;
	
	@Autowired
	@Qualifier("roleFunctionMng")
	private RoleFunctionMng roleFunctionMng;
	
	@Autowired
	@Qualifier("appFunctionMng")
	private AppFunctionMng appFunctionMng;
	
	private List<Long> ownIds;
	
	private String tree;
	public String funListForTree() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Long roleId = Long.valueOf(request.getParameter("roleId"));
		//获取角色拥有的权限
		ownIds = roleOwnFunsId(roleId); //获取角色用户的id列表
		
		List<AppFunction> funs = fetchFunListByParentId(Long.valueOf(0)); //顶级功能
		tree = treeData(funs);
		return SUCCESS;
	}
	
	private String treeData(List<AppFunction> funs){
		StringBuffer str = new StringBuffer();
		str.append('[');
		AppFunction af;
		boolean isCheck = false;
		int length = funs.size();
		for(int i = 0; i < length; i++){//循环获取顶级功能列表
			af = funs.get(i);
			if(ownIds != null)isCheck = ownIds.contains(af.getFunctionId());
			List<AppFunction> subfuns = fetchFunListByParentId(af.getFunctionId()); //获取子功能列表
			if(subfuns == null || subfuns.size() == 0) { //没有子功能类表
				str.append(leafStr(af, isCheck));
			} else { //有子列表
				str.append(foderStr(af, subfuns));
			}
			
			if(i<(length-1)) str.append(',');
		}
		str.append(']');
		return str.toString();
	}
	
	private String foderStr(AppFunction af, List<AppFunction> subFuns) {
		StringBuffer str = new StringBuffer();
		str.append("{text:");
		str.append("'"+af.getFunName()+"'");
		str.append(",id:");
		str.append("'"+af.getFunctionId()+"'");
		str.append(", cls: 'folder', children: [");
		int length = subFuns.size();
		AppFunction temp;
		boolean isCheck = false;
		for(int i = 0; i < length; i++) {
			temp = subFuns.get(i);
			if(ownIds != null) isCheck = ownIds.contains(temp.getFunctionId());
			str.append(leafStr(temp,isCheck));
			if(i < (length-1)) str.append(',');
		}		
		str.append("]}");
		return str.toString();
	}
	
	private String leafStr(AppFunction af, boolean isCheck) {
		StringBuffer str = new StringBuffer();
		str.append("{text:");
		str.append("'"+af.getFunName()+"'");
		str.append(",id:");
		str.append("'"+af.getFunctionId()+"'");
		if(isCheck) str.append(", checked: true");
		else str.append(", checked: false");
		str.append(", leaf:true}");
		return str.toString();
	}
	
	private List<Long> roleOwnFunsId(Long roleId) {
		List<Long> ids = new ArrayList<Long>();
		
		List<RoleFunction> ownFunList = roleFunctionMng.findAllByRole(roleId);
		for(Iterator<RoleFunction> i = ownFunList.iterator(); i.hasNext();) {
			RoleFunction rf = i.next();
			ids.add(rf.getFunctionId());
		}
		return ids;
	}
	
	private List<AppFunction> fetchFunListByParentId(Long parentId) {
		return appFunctionMng.findAllByParentId(parentId);
	}

	public String getTree() {
		return tree;
	}

	public void setTree(String tree) {
		this.tree = tree;
	}
}
